$(function() {
	var itemno = getUrlParam("itemno");

	getDeclareItemInfo(itemno);
	getMaterial(itemno);				//绑定明细数据
	

})

// ajax获取政务项目详细信息
function getDeclareItemInfo(itemno) {
	var infoUrl = projectPath + "declare/getInfo.do";

	$.ajax({
		url : infoUrl,
		data : {
			"itemno" : itemno
		},
		dataType : "json",
		success : function(data) {
			if (data.result == 1) {
				bindEditForm(data.datas);
			}
		}
	});
}

function bindEditForm(data) {
	$('[name="itemname"]').val(data.itemname);
	$('[name="itemno"]').val(data.itemno);
	$('[name="timelimit"]').val(data.timelimit);
	$('[name="timelimitremark"]').val(data.timelimitremark);
	$('[name="result"]').val(data.result);
	$('[name="remark"]').val(data.remark);

	if (data.iflicense == 1)
		$("input[name='iflicense']").get(0).checked = true;
	else
		$("input[name='iflicense']").get(1).checked = true;
	
	if (data.isdeclare == 1)
		$("input[name='isdeclare']").get(0).checked = true;
	else
		$("input[name='isdeclare']").get(1).checked = true;

	// 下拉列表绑定（行政许可类型）
	bindDict("itemtypecode", "DM_XKXMLX", data.itemtypecode);
}

function save() {

	var declareData = $("#declareForm").serializeObject();


	var declareMaterialData = $('#materialForm').serializeObjectArray();

	var declareMaterial = {};
	declareMaterial['dictDetail'] = 'dictDetail';
	declareMaterial['row'] = declareMaterialData;

	declareData['table'] = declareMaterial

	if (!check(declareData))
		return true;
		
	

	$.ajax({
		method : 'post',
		url : projectPath + "declare/update.do",
		data : JSON.stringify(declareData),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
	    	window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success, {
	    		onOk: function(){
	    			if(data.result == 1){
	    	    		parent.refreshData();		//刷新数据
	    	    	} 
	    		}
	    	});
		}
	});
}

function check(declareData){
	var result = true;
	

	if($('#tableMaterial').find('tbody').find('tr').not('.no-records-found').length == 0){
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>行政许可材料</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.info);
		return false;
	}
	
	
	var mustStr = "itemtypecode,itemname,materialname,typecode,verifymethod";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
	

	
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}

//查询当前字典的所有明细记录
function getMaterial(itemno){

	var infoUrl = projectPath + "declare/getMaterial.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"itemno": itemno},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				bindFormDeclareMaterialList(data.datas);
			}
      	}
	});
}

function bindFormDeclareMaterialList(data){
	for (var i = 0; i < data.length; i++) {
		bindDetailList(data[i]);
	}
}

